package com.wish.wishsystem_management.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.wishsystem_management.api.SysLog;
import com.wish.wishsystem_management.entity.SysMenu;
import com.wish.wishsystem_management.service.ISysMenuService;
import dto.systemDto.SysMenuDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import vo.ResultEntity;

import java.security.Principal;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author zhaiwj
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/sysMenu")
@Slf4j
public class SysMenuController {

    //菜单service
    @Autowired
    private ISysMenuService iSysMenuService;


    //列表展示
    @RequestMapping("/list")
    public ResultEntity list(SysMenuDto sysMenuDto) {
        //分页
        PageHelper.startPage(sysMenuDto.getPageNum(), sysMenuDto.getPageSize());
        //查询
        List<SysMenu> list = iSysMenuService.listAll(sysMenuDto);
        //封装
        PageInfo<SysMenu> info = new PageInfo<>(list);
        return ResultEntity.ok("200", "列表查询成功", info);
    }

    //查询父级菜单（一二级菜单）（菜单类型type不为2--按钮）
    @RequestMapping("/parentList")
    public ResultEntity parentList(SysMenuDto sysMenuDto) {
        //查询
        List<SysMenu> list = iSysMenuService.parentList();
        return ResultEntity.ok("200", "父级菜单查询成功", list);
    }

    //添加编辑
    @SysLog("保存菜单")
    @RequestMapping("/edit")
    public ResultEntity edit(@RequestBody SysMenuDto sysMenuDto) {
        //转换
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDto, sysMenu);
        //判断
        if (sysMenu.getMenuId() != null) {
            //修改
            iSysMenuService.updateById(sysMenu);
            return ResultEntity.ok("200", "修改成功");
        } else {
            //添加
            iSysMenuService.save(sysMenu);
            return ResultEntity.ok("200", "添加成功");
        }
    }

    //根据Id查询回显
    @RequestMapping("/selectById")
    public ResultEntity selectById(Long menuId) {
        //查询
        SysMenu menu = iSysMenuService.selectByMid(menuId);
        return ResultEntity.ok("200", "根据id查询成功", menu);
    }

    //删除
    @SysLog("删除菜单")
    @RequestMapping("/delete")
    public ResultEntity delete(Long mids[]) {
        try {
            if (mids.length > 0) {
                //循环判断
                for (Long mid : mids) {
                    //查询系统菜单
                    SysMenu menu = iSysMenuService.selectMidGetType(mid);
                    Integer type = menu.getType();
                    //判断是否是系统菜单
                    if (type == 0) {
                        return ResultEntity.error("401", "系统菜单不能删除！", null);
                    }
                    //判断是否有子菜单或按钮
                    /*if(type!=2){
                        return ResultEntity.error("401","请先删除子菜单或按钮",null);
                    }*/
                    //判断是否有子菜单或按钮
                    List<SysMenu> menuList = iSysMenuService.queryListParentId(mid);
                    /*for (SysMenu sysMenu : menuList) {
                        System.out.println(menuList);
                    }*/
                    if (menuList.size() > 0) {
                        return ResultEntity.error("401", "请先删除子菜单或按钮", null);
                    }
                    if (menuList.size() > 0) {
                        return ResultEntity.error("401", "请先删除子菜单或按钮", null);
                    }
                }

                //批量删除
                iSysMenuService.removeByIds(Arrays.asList(mids));
                return ResultEntity.ok("200", "删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultEntity.error("401", "删除失败");
    }

    @RequestMapping("getMenus")
    public ResultEntity getMenus(Principal member) {
        log.info("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" + member);
        String name = member.getName();

        List<SysMenu> menus = iSysMenuService.getMenus(name);

        return ResultEntity.ok(menus);
    }

}

